// // 入参格式参考：
// const sourceUrl = "https://www.taobao.com?a=1&b=2&c=3&d#name";
// // 出参格式参考：
// const params = {
//   a: "1",
//   b: "2",
//   c: "3",
//   d: null
// };

// function queryParse(urlStr) {
//     const reg = new RegExp("(^|&)" + urlStr + "=([^&]*)(&|$)", "i");
//     // const urlObj = window.location;
//     var r = urlStr.substr(1).match(reg);
//     if (r != null) return unescape(r[2]);
//     return null;
//   }
//   console.log(queryParse("https://www.taobao.com"))
//   console.log(queryParse("https://www.taobao.com?a=1&b=2&c=3&d#name"))


// async function printList(list, delay) {
//   function sleep(d, res) {
//     return new Promise((resolve, reject) => {
//       setTimeout(() => {
//         resolve(res);
//       }, d*1000);
//     });
//   }

//   list.forEach(async (res) => {
//     let u = await sleep(delay, () =>{});
//     console.log(u);
//   })

//   // for (let i = 0; i < list.length; i++) {
//   //   let u = await sleep(delay, list[i]);
//   //   console.log(u);
//   // }
// }

// printList([1, 2, 3], 3)




class Calendar {
  /**
   * 此处写代码逻辑
   */
  constructor() {
    this.books = [];
  }

  book(a, b) {
    if (a === b && a === 0) return false;
    if (a > 31 || b > 31) return false;
    // this.books.push([a, b]);
    let _books = [...this.books, [a, b]]

    return this.validate(_books);
  }

  validate(list) {
    list.sort((a, b) => a[1] - b[1]);
    let max = list[0][1];
    let count = 0;
    for (let i = 0; i < list.length; i++) {
      if (list[i][0] > max) {
        count++;
        max = list[i][1];
      }
    }
    console.log("=====", count, list, this.books);
    if (count + 2 < list.length) {
      return false;
    }
    this.books = list
    return true;
  }
}


const mySchedule = new Calendar();
console.log(mySchedule.book(0, 0) === false);
console.log(mySchedule.book(32, 35) === false);
console.log(mySchedule.book(1, 10) === true);
console.log(mySchedule.book(8, 14) === true);
console.log(mySchedule.book(12, 16) === true);
console.log(mySchedule.book(22, 30) === true);
console.log(mySchedule.book(2, 9) === false);
console.log(mySchedule.book(18, 20) === true);
console.log(mySchedule.book(13, 17) === false);



